package com.hlw.hr_man.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.hlw.hr_man.dao.AddDeptDao;
import com.hlw.hr_man.util.JDBCUtil;

public class AddDeptDaoImpl implements AddDeptDao{

	@Override
	public int addDepts(String dept_name, String dept_describe, String dept_createtime) {
		
		int flag = 0;
		Connection con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("INSERT INTO hr_department(dept_name,dept_describe,dept_createtime,dept_isdel) SELECT ?,?,?,0 FROM hr_department WHERE NOT EXISTS(SELECT dept_name FROM hr_department WHERE dept_name = ?)limit 1;");
		PreparedStatement pst;
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setString(1,dept_name);
			pst.setString(2, dept_describe);
			pst.setString(3,dept_createtime);
			pst.setString(4, dept_name);
			int i = pst.executeUpdate();
			if(i>0){
				 flag = 1;
			}else{
				flag = 0;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return flag;
	}

}
